package co.edu.usbcali.raju.games.chess.server;

import java.io.FileInputStream;
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Properties;

import jpl.Query;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import co.edu.usbcali.raju.db.RajuConnection;
import co.edu.usbcali.raju.games.chess.controller.ChessController;
import co.edu.usbcali.raju.games.chess.engine.TestText;
import co.edu.usbcali.raju.games.chess.model.AlphaBetaSearch;
import co.edu.usbcali.raju.games.chess.model.EvaluateForce;
import co.edu.usbcali.raju.games.chess.model.EvaluateThreat;
import co.edu.usbcali.raju.model.ComplexEvaluator;

public class ChessServer {
	public static final String SERVER="server";
	public static final String PORT="port";
	public static final String DB_PROPERTIES="dbProperties";
	static Logger logger;
//	static Properties properties;
	
	   public static void main(String args[]) {
			logger=Logger.getLogger(TestText.class.getName());
			logger.setLevel(Level.DEBUG);
			Properties properties = new Properties();
			try {
			    properties.load(new FileInputStream("properties/rajuServer.properties"));
			} catch (IOException e) {
				e.printStackTrace();
				System.exit(0);
			}
			try{
				new RajuConnection(properties.getProperty(DB_PROPERTIES));
			}catch(Exception e){
				logger.debug("Error conectandose a la base de datos"+ e.getMessage());
			}
			try{
			initProlog();
			ComplexEvaluator evaluator=new ComplexEvaluator();
			evaluator.put(EvaluateForce.getInstance(), 0.5d);
			evaluator.put(EvaluateThreat.getInstance(), 0.5d);
			ChessController controller=new ChessController(new AlphaBetaSearch(20),evaluator);
			XBoardAdapter engine=new XBoardAdapter(controller);

			String portNum = properties.getProperty(PORT), registryURL;
		 		   // code for obtaining RMI port number value omitted
		         startRegistry(1099);
		         // register the object under the name "some"
		         registryURL = "rmi://"+properties.getProperty(SERVER)+":" + portNum + "/xboard";
		         Naming.rebind(registryURL, engine);
		         System.out.println("Some Server ready.");
		      }// end try
		      catch (Exception re) {
		         System.out.println(
		            "Exception in SomeServer.main: " + re);
		      } // end catch
	   }
		      
		      private static void startRegistry(int RMIPortNum)
		      throws RemoteException{
		      try {
		         Registry registry = LocateRegistry.getRegistry(RMIPortNum);
		         registry.list( );  
					// The above call will throw an exception
		         // if the registry does not already exist
		      }
		      catch (RemoteException ex) {
		         // No valid registry at that port.
		         System.out.println(
		            "RMI registry cannot be located at port " 
		            + RMIPortNum);
		         Registry registry = LocateRegistry.createRegistry(RMIPortNum);
		         System.out.println(
		            "RMI registry created at port " + RMIPortNum);
		      }
		   } // end startRegistry
		  	public static void initProlog(){
				String t3 = "consult('chessEval3.pl')";
				Query q3 = new Query(t3);
				q3.hasSolution();
			//	System.out.println( t3 + " " + (q3.hasSolution() ? "succeeded" : "failed") );
			}

}
